package com.ltmonitor.web.controller.video;

import com.ltmonitor.entity.BasicData;
import com.ltmonitor.entity.TerminalCommand;
import com.ltmonitor.entity.TerminalParam;
import com.ltmonitor.entity.VehicleData;
import com.ltmonitor.service.IVideoFileItemService;
import com.ltmonitor.service.IVideoServerConfigService;
import com.ltmonitor.service.JT808Constants;
import com.ltmonitor.util.DateUtil;
import com.ltmonitor.util.StringUtil;
import com.ltmonitor.video.entity.VideoFileItem;
import com.ltmonitor.video.entity.VideoServerConfig;
import com.ltmonitor.web.controller.command.TerminalCommandAction;
import com.ltmonitor.web.controller.command.TerminalParamAction;
import com.ltmonitor.web.util.JsonMessage;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * 下发查询终端音视频资源列表命令
 * 
 * @author DELL
 * 
 */
@Controller
@RequestMapping("/videoTerminalParam")
public class VideoTerminalParamAction extends TerminalParamAction {


	/**
	 * 打开音视频参数设置页面
	 * @param vehicleId
	 * @param m
	 * @return
	 */
	@RequestMapping("/viewAvParam.action")
	public String viewAvParam(long vehicleId, Model m) {
		m.addAttribute("vehicleId", vehicleId);
		return "/videoTerminalParam/avParam";
	}

	/**
	 * 打开通道列表参数设置页面
	 * @param vehicleId
	 * @param m
	 * @return
	 */
	@RequestMapping("/viewAvChannelListParam.action")
	public String viewChannelListParam(long vehicleId, Model m) {
		m.addAttribute("vehicleId", vehicleId);
		return "/videoTerminalParam/avChannelList";
	}


	/**
	 * 单独通道音视频参数设置
	 * @param vehicleId
	 * @param m
	 * @return
	 */
	@RequestMapping("/viewChannelAvParam.action")
	public String viewChannelAvParam(long vehicleId, Model m) {
		m.addAttribute("vehicleId", vehicleId);
		return "/videoTerminalParam/channelAvParam";
	}


	/**
	 * 特殊类型报警录像定义
	 * @param vehicleId
	 * @param m
	 * @return
	 */
	@RequestMapping("/viewSpecialAlarmVideoParam.action")
	public String viewSpecialAlarmVideoParam(long vehicleId, Model m) {
		m.addAttribute("vehicleId", vehicleId);
		return "/videoTerminalParam/specialAlarmVideoParam";
	}

	/**
	 * 视频分析报警参数定义
	 * @param vehicleId
	 * @param m
	 * @return
	 */
	@RequestMapping("/viewVideoAnalyzeAlarmParam.action")
	public String viewVideoAnalyzeAlarmParam(long vehicleId, Model m) {
		m.addAttribute("vehicleId", vehicleId);
		return "/videoTerminalParam/videoAnalyzeAlarmParam";
	}

	/**
	 * 唤醒模式定义
	 * @param vehicleId
	 * @param m
	 * @return
	 */
	@RequestMapping("/viewWakeupModeParam.action")
	public String viewWakeupModeParam(long vehicleId, Model m) {
		m.addAttribute("vehicleId", vehicleId);
		return "/videoTerminalParam/wakeupMode";
	}
	/**
	 * 唤醒模式定义
	 * @param vehicleId
	 * @param m
	 * @return
	 */
	@RequestMapping("/viewAlarmShielParam.action")
	public String viewAlarmShielParam(long vehicleId, Model m) {
		m.addAttribute("vehicleId", vehicleId);
		return "/videoTerminalParam/videoAlarmShield";
	}



	/**
	 * 音视频参数设置
	 * @param vehicleId
	 * @param realTimeCodecMode   实时流编码模式
	 * @param resolution 实时流分辨率
	 * @param keyFrameInterval 实时流关键帧间隔
	 * @param frameRate 实时流目标帧率
	 * @param bitRate 实时流目标码率
	 * @param storageCodecMode 存储流编码模式
	 * @param storageResolution 存储流分辨率
	 * @param storageKeyFrameInterval 存储流关键帧间隔
	 * @param storageFrameRate 存储流目标帧率
	 * @param storageBitRate 存储流目标码率
	 * @param osdText OSD字幕叠加设置
	 * @param enableAudio 音频输出
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/configAvParam.action")
	public JsonMessage  configAvParam(long vehicleId, int realTimeCodecMode, int resolution,
									  int keyFrameInterval,int frameRate,int bitRate,int storageCodecMode,
									  int storageResolution,int storageKeyFrameInterval,int storageFrameRate,
									  int storageBitRate,
									  String osdText,int enableAudio) {
		try {
			StringBuilder sb = new StringBuilder();
			long osdValue = StringUtil.convertBitStringToInt(osdText,16);
			int paramId = 0x0075;
			sb.append(paramId).append(",");
			String sep = "|";
			sb.append(realTimeCodecMode).append(sep).append(resolution).append(sep)
					.append(keyFrameInterval).append(sep).append(frameRate).append(sep)
					.append(bitRate).append(sep).append(storageCodecMode).append(sep)
					.append(storageResolution).append(sep).append(storageKeyFrameInterval).append(sep)
					.append(storageFrameRate).append(sep).append(storageBitRate).append(sep).append(osdValue).append(sep)
					.append(enableAudio);
			TerminalCommand tc = this.sendCommand(vehicleId, sb.toString(),JT808Constants.CMD_CONFIG_PARAM);
			return json(true, tc.getEntityId());
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return json(false, ex.getMessage());
		}
	}

	@ResponseBody
	@RequestMapping("/configChannelAvParam.action")
	public JsonMessage  configChannelAvParam(long vehicleId, String avParam)
	{
		try {
			StringBuilder sb = new StringBuilder();
			int paramId = 0x0077;
			sb.append(paramId).append(",").append(avParam);
			TerminalCommand tc = this.sendCommand(vehicleId, sb.toString(),JT808Constants.CMD_CONFIG_PARAM);
			return json(true, tc.getEntityId());
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return json(false, ex.getMessage());
		}
	}


	/**
	public Map<String, String> getChannelAvParamMap() {
		Map<String, String> avParamMap = (Map<String, String>) this.getFromSession(
				"Channel_AvParam");
		if (avParamMap == null) {
			avParamMap = new HashMap<String, String>();
			this.putSession("Channel_AvParam", avParamMap);
		}
		return avParamMap;
	}*/

	@ResponseBody
	@RequestMapping("/configVideoAlarmShield.action")
	public JsonMessage  configVideoAlarmShield(long vehicleId, int[] alarmType) {
		try {
			long bitValue = StringUtil.convertBitArrayToLong(alarmType, 32); // 将位数组转换为字节值
			int paramId = 0x007A;
			StringBuilder sb = new StringBuilder();
			sb.append(paramId).append(",").append(bitValue);
			TerminalCommand tc = this.sendCommand(vehicleId, sb.toString(),JT808Constants.CMD_CONFIG_PARAM);
			return json(true, tc.getEntityId());
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return json(false, ex.getMessage());
		}
	}

	/**
	 * 设置特殊报警类型录像参数定义
	 * @param vehicleId
	 * @param storeThreshold 存储阈值
	 * @param duration 持续时间
	 * @param advanceTime 特殊报警发生前标识的录像起始时间 单位为分钟
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/configSpecialAlarmVideoParam.action")
	public JsonMessage  configSpecialAlarmVideoParam(long vehicleId, int storeThreshold, int duration,
									  int advanceTime) {
		try {
			StringBuilder sb = new StringBuilder();
			int paramId = 0x0079;
			sb.append(paramId).append(",");
			String sep = "|";
			sb.append(storeThreshold).append(sep).append(duration).append(sep)
					.append(advanceTime);
			TerminalCommand tc = this.sendCommand(vehicleId, sb.toString(),JT808Constants.CMD_CONFIG_PARAM);
			return json(true, tc.getEntityId());
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return json(false, ex.getMessage());
		}
	}

	@ResponseBody
	@RequestMapping("/configVideoAnalyzeAlarmParam.action")
	public JsonMessage  configVideoAnalyzeAlarmParam(long vehicleId, int peopleNum, int tiredLevel) {
		try {
			StringBuilder sb = new StringBuilder();
			int paramId = 0x007B;
			sb.append(paramId).append(",");
			String sep = "|";
			sb.append(peopleNum).append(sep).append(tiredLevel);
			TerminalCommand tc = this.sendCommand(vehicleId, sb.toString(),JT808Constants.CMD_CONFIG_PARAM);
			return json(true, tc.getEntityId());
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return json(false, ex.getMessage());
		}
	}

	@ResponseBody
	@RequestMapping("/configAvChannelListParam.action")
	public JsonMessage  configAvChannelListParam(long vehicleId, String inserted) {
		try {
			StringBuilder sb = new StringBuilder();
			int paramId = 0x0076;
			sb.append(paramId).append(",");
			String sep = "&";
			JSONArray arry = JSONArray.fromObject(inserted);
			for (int m = 0; m < arry.size(); m++) {
				JSONObject mi = arry.getJSONObject(m);
				int physicalChannelId = mi.getInt("physicalChannelId");
				int logicalChannelId = mi.getInt("logicalChannelId");
				int channelType = mi.getInt("channelType");
				int panelType = mi.getInt("panelType");

				if(m > 0)
					sb.append("|");
				sb.append(physicalChannelId).append(sep).append(logicalChannelId).append(sep)
						.append(channelType).append(sep).append(panelType);

			}
			TerminalCommand tc = this.sendCommand(vehicleId, sb.toString(),JT808Constants.CMD_CONFIG_PARAM);
			return json(true, tc.getEntityId());
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return json(false, ex.getMessage());
		}
	}


	@ResponseBody
	@RequestMapping("/configWakeupMode.action")
	public JsonMessage  configWakeupMode(long vehicleId, int[] wakeupMode, int[] wakeupCondition, int[] wakeupDay, int[] enableTimeSpan, String[] startTime, String[] endTime) {
		try {
			long wakeupBitValue = StringUtil.convertBitArrayToLong(wakeupMode, 3);
			long wakeupConditionBitValue = StringUtil.convertBitArrayToLong(wakeupCondition, 3);
			long wakeupDayBitValue = StringUtil.convertBitArrayToLong(wakeupDay, 7);
			long enableTimeSpanBitValue = StringUtil.convertBitArrayToLong(enableTimeSpan, 4);
			StringBuilder sb = new StringBuilder();
			int paramId = 0x007C;
			sb.append(paramId).append(",");
			String sep = "|";
			sb.append(wakeupBitValue).append(sep).append(wakeupConditionBitValue).append(sep)
					.append(wakeupDayBitValue).append(sep).append(enableTimeSpanBitValue);
			for(int m = 0; m < startTime.length; m++)
			{
				sb.append(sep).append(startTime[m].replace(":","")).append(sep).append(endTime[m].replace(":",""));
			}
			TerminalCommand tc = this.sendCommand(vehicleId, sb.toString(),JT808Constants.CMD_CONFIG_PARAM);
			return json(true, tc.getEntityId());
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return json(false, ex.getMessage());
		}
	}
	@ResponseBody
	@RequestMapping("/sendParamQueryCommand.action")
	public JsonMessage  sendParamQueryCommand(long vehicleId, int paramId) {
		try {
			StringBuilder sb = new StringBuilder();
			sb.append(paramId);
			TerminalCommand tc = this.sendCommand(vehicleId, sb.toString(),JT808Constants.CMD_QUERY_SPECIAL_PARAM);
			return json(true, tc.getEntityId());
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			return json(false, ex.getMessage());
		}
	}




	private TerminalCommand sendCommand(long vehicleId, String cmdData, int cmdType)
	{
		TerminalCommand tc = new TerminalCommand();
		tc.setVehicleId(vehicleId);
		tc.setCmdData(cmdData.toString());
		tc.setCmdType(cmdType);
		SendCommand(tc);
		return tc;
	}



}
